/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceAttributeDefense extends GameResourceAttribute
{
	public GameResourceAttributeDefense()
	{	
		setType(Types.Defense);
		setAbilityType(GameAbilities.Dexterity);
	}
	
	public GameResourceAttributeDefense(Parcel in)
	{
		super(in);
		setType(Types.Defense);
		setAbilityType(GameAbilities.Dexterity);
	}
	
	public static final Parcelable.Creator<GameResourceAttributeDefense> CREATOR = new Parcelable.Creator<GameResourceAttributeDefense>() 
	{
		public GameResourceAttributeDefense createFromParcel(Parcel in) 
		{
		    return new GameResourceAttributeDefense(in);
		}
		
		public GameResourceAttributeDefense[] newArray(int size) 
		{
		    return new GameResourceAttributeDefense[size];
		}
	};
	
	@Override
	public int getModifierTotal()
	{
		return getModifier() + getAbility().getModifier();
	}
	
	@Override
	public int getValue()
	{
		int value = getValueBase() + getModifier() + getMisc();
		value = checkCondition(value, true);
		return value;
	}
	
	public int getValueFlatFooted()
	{
		int value = _valueFlatFooted + getModifier() + getMisc();
		value = checkCondition(value, (_dexModifierIncludeFlatFooted > 0 ? true : false));
		return value;
	}
	
	public int getValueShieldless()
	{
		int value = _valueShieldless + getModifier() + getMisc();
		value = checkCondition(value, true);
		return value;
	}
	
	public int getValueTouch()
	{
		int value = _valueTouch + getModifier() + getMisc();
		value = checkCondition(value, true);
		return value;
	}
	
	@Override
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		super.load(data);
		
		if (data == null)
			return null;
		
		setDexModifierIncludeFlatFooted(getJSONInt(data, "dexModifierIncludeFlatFooted"));
		setDexModifierMax(getJSONInt(data, "maxDexModifier"));
		setValueFlatFooted(getJSONInt(data, "valueFlatFooted"));
		setValueShieldless(getJSONInt(data, "valueShieldless"));
		setValueTouch(getJSONInt(data, "valueTouch"));
	    
	    return data;
	}
	
	@Override
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = super.save();

	    data.put("dexModifierIncludeFlatFooted", _dexModifierIncludeFlatFooted);
	    data.put("dexModifierMax", _dexModifierMax);
	    data.put("valueFlatFooted", _valueFlatFooted);
	    data.put("valueShieldless", _valueShieldless);
	    data.put("valueTouch", _valueTouch);
	    
	    return data;
	}
	
	public void setDexModifierIncludeFlatFooted(int value)
	{
		_dexModifierIncludeFlatFooted = value;
	}
	
	public void setDexModifierMax(int value)
	{
		_dexModifierMax = value;
	}
	
	public void setValueFlatFooted(int value)
	{
		_valueFlatFooted = value;
	}
	
	public void setValueShieldless(int value)
	{
		_valueShieldless = value;
	}
	
	public void setValueTouch(int value)
	{
		_valueTouch = value;
	}
	
	protected int checkCondition(int value, boolean hasDexterity)
	{
		return getFolder().checkConditionDefense(this, value, hasDexterity);
	}

	@Override
	protected void readFromParcelTransformEx(Parcel in) 
		throws Throwable
	{
		try
		{
			super.readFromParcelTransformEx(in);
			
			setDexModifierIncludeFlatFooted(in.readInt());
			setDexModifierMax(in.readInt());
			setValueFlatFooted(in.readInt());
			setValueShieldless(in.readInt());
			setValueTouch(in.readInt());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransformEx", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransformEx(Parcel dest, int flags)
		throws Throwable
	{
		try
		{
			super.writeToParcelTransformEx(dest, flags);
			
			dest.writeInt(_dexModifierIncludeFlatFooted);
			dest.writeInt(_dexModifierMax);
			dest.writeInt(_valueFlatFooted);
			dest.writeInt(_valueShieldless);
			dest.writeInt(_valueTouch);
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransformEx", tr);
    		throw tr;
    	}
	}

	private int _dexModifierIncludeFlatFooted;
	private int _dexModifierMax;
	private int _valueFlatFooted;
	private int _valueShieldless;
	private int _valueTouch;

	private static final String TAG = GameResourceAttributeDefense.class.getSimpleName();
}
